﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace TimKen.BussLogic
{
    /// <summary>
    /// 地址管理类
    /// </summary>
    public class AddressManager
    {
        private readonly TimKen.DataAccess.AddressDAO dal = new TimKen.DataAccess.AddressDAO();
        public AddressManager()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string AddressCode, string AdresType, string CardCode)
        {
            return dal.Exists(AddressCode, AdresType, CardCode);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.Address model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Address model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string AddressCode, string AdresType, string CardCode)
        {
            dal.Delete(AddressCode, AdresType, CardCode);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Address GetModel(string AddressCode, string AdresType, string CardCode)
        {
            return dal.GetModel(AddressCode, AdresType, CardCode);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获取货运地址
        /// </summary>
        /// <param name="cardCode"></param>
        /// <returns></returns>
        public List<TimKen.DataEntity.Address> GetShipToAddress(string cardCode, int LanguageID) 
        {
            return dal.GetAddressList(string.Format(" CardCode='{0}' and (adresType='S' OR adresType='D')   and LanguageID={1}", cardCode, LanguageID));
        }

        public List<TimKen.DataEntity.Address> GetShipToAddress(string cardCode)
        {
            return dal.GetAddressList(string.Format(" CardCode='{0}' and adresType='S'", cardCode));
        }
        /// <summary>
        /// 获取账单寄送地址
        /// </summary>
        /// <param name="cardCode"></param>
        /// <returns></returns>
        public List<TimKen.DataEntity.Address> GetBillToAddress(string cardCode,int LanguageID) 
        {
            return dal.GetAddressList(string.Format(" CardCode='{0}' and adresType='B' and LanguageID={1}",cardCode,LanguageID));
        }

        public List<TimKen.DataEntity.Address> GetBillToAddress(string cardCode)
        {
            return dal.GetAddressList(string.Format(" CardCode='{0}' and adresType='B'", cardCode));
        }


        /// <summary>
        /// 如果根据ShipTo 或者 BillTo获取不到时使用该方法获取
        /// </summary>
        /// <param name="cardCode"></param>
        /// <returns></returns>
        public List<TimKen.DataEntity.Address> GetAddress(string cardCode, int LanguageID) 
        {
            return dal.GetAddressList(" CardCode='" + cardCode + "'");
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        public string GetAddressString(TimKen.DataEntity.Address address) {
            //Country State  City County Block Street  Building( ZipCode ) 
            StringBuilder builder = new StringBuilder();
            builder.Append(address.Country);
            builder.Append(address.State);
            builder.Append(address.City);
            builder.Append(address.County);
            builder.Append(address.Block);
            builder.Append(address.Street);
            builder.Append(address.Building);
            builder.Append("(");
            builder.Append(address.ZipCode);
            builder.Append(")");

            return builder.ToString();
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法
    }
}
